// pages/mine/lingshi_order/payment.js
var App = getApp();
var userWallet = 0;//使用余额
var orginMoney = 0;//订单金额
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'',
    orderMoney:'0.00', //订单
    originPrice:'0.00',
    wallet:'0.00',
    yueSwicth:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.order_id,
      orderMoney: options.total,
      wallet: options.account
    })
    orginMoney = this.data.orderMoney;

    if (this.data.wallet>0) {//使用余额
      this.setData({
        yueSwicth: true
      })
      if (this.data.orderMoney - this.data.wallet > 0) {
        userWallet = parseFloat(this.data.wallet).toFixed(2);
        this.setData({
          orderMoney: parseFloat(this.data.orderMoney - this.data.wallet).toFixed(2)
        })
      } else {
        userWallet = parseFloat(this.data.orderMoney).toFixed(2);
        this.setData({
          orderMoney: '0.00'
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //余额开关
  userYueSwitch: function () {
    if (this.data.wallet == 0) {
      return false;
    }
    this.setData({
      yueSwicth: !this.data.yueSwicth
    })
    if (this.data.yueSwicth) {//使用余额
      if (this.data.orderMoney - this.data.wallet > 0) {
        userWallet = parseFloat(this.data.wallet).toFixed(2);
        this.setData({
          orderMoney: parseFloat(this.data.orderMoney - this.data.wallet).toFixed(2)
        })
      } else {
        userWallet = parseFloat(this.data.orderMoney).toFixed(2);
        this.setData({
          orderMoney: '0.00'
        })
      }
    }else{
      userWallet = 0;
      this.setData({
        orderMoney: orginMoney
      })
    }
  },

  //提交支付
  payMethod: function () {
    let that = this;
    let payWay = '';
    if (that.data.payChecked && that.data.wallet > 0 && that.data.orderMoney == 0) {
      payWay = 'account';
    } else if (that.data.payChecked && that.data.wallet > 0 && that.data.orderMoney > 0) {
      payWay = 'mixed';
    } else if (that.data.orderMoney == 0) {
      payWay = 'account';
    } else {
      payWay = 'online';
    }

    wx.request({
      url: App.data.globalAPI + '/order/mall/pay',
      data: {
        token: wx.getStorageSync('token'),
        pay_type: payWay,//在线支付: online 余额支付: account 混合支付: mixed,
        pay_method: payWay == 'account' ? 'account' : 'wxlite',
        order_no: that.data.orderId,
        pay: parseFloat(orginMoney).toFixed(2), //实付款
        account_pay: userWallet,//余额支付金额 
        online_pay: parseFloat(that.data.orderMoney).toFixed(2),//在线支付金额 
        client_type: 'wxlite'
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          var payArr = res.data.data.pay;
          var orderArr = res.data.data.order;
          if (payWay == 'account') {
            wx.showToast({
              title: '支付成功'
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/mine/lingshi_order/lingshi_detail?source=mall &type=1 &order_no='+ that.data.orderId,
              })
            }, 600)
            App.getTokenLogin();
            return false;
          }
          wx.requestPayment({
            timeStamp: payArr.timeStamp,
            nonceStr: payArr.nonceStr,
            package: payArr.package,
            signType: payArr.signType,
            paySign: payArr.paySign,
            success(res) {
              wx.redirectTo({
                url: '/pages/mine/lingshi_order/lingshi_detail?source=mall &type=2 &order_no=' + that.data.orderId,
              })
              App.getTokenLogin();
            },
            fail(res) {
              wx.showToast({
                icon: 'none',
                title: '支付已取消'
              })
              setTimeout(function(){
                wx.navigateTo({
                  url: '/pages/mine/lingshi_order/lingshi_detail?source=mall &type=1 &order_no=' + that.data.orderId
                })
              },600)
            }
          })
        }
        else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }

      }, fail(res) {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

})